int m1_in1 = 2;      /* IN1 of motor1 */
int m1_in2 = 4;      /* IN2 of motor2 */
int m1_pwm = 3;      /* PWM of motor3 */


void setup(){
Serial.begin(9600);
  pinMode(m1_in1, OUTPUT);
  pinMode(m1_in2, OUTPUT);
  pinMode(m1_pwm, OUTPUT);
  digitalWrite(m1_in1, LOW);
  digitalWrite(m1_in2, LOW);
  analogWrite(m1_pwm, 0);
  
}
void loop(){
 if(Serial.available()>0){
    byte dir = Serial.read(); 
    byte sp = Serial.read();
     if( dir == 1){                  /* Move motor1 forward */
          digitalWrite(m1_in1, LOW);
          digitalWrite(m1_in2, HIGH);
          analogWrite(m1_pwm, sp);
        }
        else if(dir == 2 ){             /* Move motor1 backward */
          digitalWrite(m1_in1, HIGH);
          digitalWrite(m1_in2, LOW);
          analogWrite(m1_pwm, sp);
        }
        else {
          digitalWrite(m1_in1, HIGH);
          digitalWrite(m1_in2, LOW);
          analogWrite(m1_pwm, 0);
        }
  }
}
